package cn.lsnu.common.dict.controller;

import cn.lsnu.common.dict.entity.Dict;
import cn.lsnu.common.dict.service.DictService;
import cn.lsnu.common.interfacetools.interfacecontroller.BaseController;
import cn.lsnu.common.interfacetools.interfaceentity.Page;
import cn.lsnu.common.sys.entity.MenuEntity;
import cn.lsnu.common.sys.service.MenuService;
import cn.lsnu.utils.Global;
import cn.lsnu.utils.Guuid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping({"/admin/dict","/app/dict"})
public class DictController extends BaseController {

    @Autowired
    private DictService dictService;

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/allDict")
    public String showDict(Model model){
        List<Dict> dictList = dictService.showDict();
        model.addAttribute("dictList",dictList);
        return "webpage/modules/admin/dict/showAllDict";
    }

    @RequestMapping(value = "/toAddDict")
    public String addDict(){
        return "webpage/modules/admin/dict/dict-add";
    }

    @RequestMapping(value = "/addDict")
    @ResponseBody
    public String saveDict(Dict dict){
        dict.setDictId(Guuid.uuid());
        System.out.println(dict);
        int result = dictService.saveDict(dict);
        return "success";
    }

    @RequestMapping(value = "/delDict")
    public String delDict(String dictId){
        int result = dictService.deleteDictById(dictId);
        return "redirect:allDict";
    }
    @RequestMapping(value = "/listDict")
    public
    @ResponseBody List<Dict> listDict(Dict entity) throws Exception {
        List<Dict> dictList = dictService.findIdentify(entity);
        return dictList;
    }

    @RequestMapping("/list")
    public String listInfos(Model model, HttpSession session, MenuEntity entity)throws Exception{
        String firstTitle = entity.getFirstTitle();
        //每个-list页面的id
        model.addAttribute("pageId",entity.getMenuId());
        //获取页面的所有信息
        String userCode = (String) session.getAttribute(Global.SESSION_ID);
        entity.setUserCode(userCode);
        entity = menuService.getPageMenu(entity);
        entity.setFirstTitle(firstTitle);
        entity.setSecondTitle("信息管理列表");
        model.addAttribute("pageMenuMsg",entity);
        return "webpage/common/dict/dict-list";
    }
    @RequestMapping({"/listjson"})
    public
    @ResponseBody
    Page<Dict> pages(Dict page)throws  Exception{
        Page<Dict> pageList = dictService.findPage(page);
        return pageList;
    }

    @RequestMapping("/updmsg")
    public String updMsg(String key,Model model)throws Exception{
        Dict entity = dictService.get(key);
        model.addAttribute("upddict",entity);
        return "webpage/common/dict/dict-upd";
    }

    @RequestMapping("/upd")
    public void upd(Dict entity)throws Exception{
        dictService.update(entity);
    }
}
